package io.hansan.oli.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import io.hansan.oli.entity.InboundOrder;
 import io.hansan.oli.mapper.InboundOrderMapper;
 import io.hansan.oli.dao.MBGMCR04DAO;
 import io.hansan.oli.service.InboundOrderService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
 import java.io.StringReader;
 import java.time.LocalDateTime;
 
 /**
  * @Author ：何汉叁
  * @Date ：2025/5/22 16:39
  * @Description：入库订单服务实现类
  */
 @Service
 public class InboundOrderServiceImpl implements InboundOrderService {
     @Autowired
     private InboundOrderMapper inboundOrderMapper;
 
     @Override
     public InboundOrder processXmlOrder(String xmlContent) throws JAXBException {
         JAXBContext jaxbContext = JAXBContext.newInstance(MBGMCR04DAO.class);
         Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
         MBGMCR04DAO MBGMCR04DAO = (MBGMCR04DAO) unmarshaller.unmarshal(new StringReader(xmlContent));
 
         return saveOrUpdateOrder(MBGMCR04DAO);
     }
 
     private InboundOrder saveOrUpdateOrder(MBGMCR04DAO MBGMCR04DAO) {
         MBGMCR04DAO.E1Bp2017GmItemCreate itemCreate = MBGMCR04DAO.getIdoc().getE1Mbgmcr().getE1Bp2017GmItemCreate();
 
         String orderId = itemCreate.getOrderId();
         String material = itemCreate.getMaterial();
         String plant = itemCreate.getPlant();
         String batch = itemCreate.getBatch();
         Double quantity = Double.parseDouble(itemCreate.getEntryQnt().trim());
         String unit = itemCreate.getEntryUom();
 
         InboundOrder inboundOrder = new InboundOrder();
         inboundOrder.setOrderId(orderId);
         inboundOrder.setMaterial(material);
         inboundOrder.setPlant(plant);
         inboundOrder.setBatch(batch);
         inboundOrder.setQuantity(quantity);
         inboundOrder.setUnit(unit);
         inboundOrder.setCreateTime(LocalDateTime.now());
         inboundOrder.setUpdateTime(LocalDateTime.now());
         inboundOrder.setDeleted(false);
 
         QueryWrapper<InboundOrder> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("order_id", orderId);
         InboundOrder existing = inboundOrderMapper.selectOne(queryWrapper);
 
         if (existing != null) {
             inboundOrder.setId(existing.getId());
             inboundOrder.setCreateTime(existing.getCreateTime());
             inboundOrder.setUpdateTime(LocalDateTime.now());
             inboundOrderMapper.updateById(inboundOrder);
         } else {
             inboundOrderMapper.insert(inboundOrder);
         }
 
         return inboundOrder;
     }
 
     @Override
     public InboundOrder getByOrderId(String orderId) {
         QueryWrapper<InboundOrder> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("order_id", orderId);
         return inboundOrderMapper.selectOne(queryWrapper);
     }
 }